/***************************************************************
*  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
*  All rights reserved
*
*  This file is part of the GroupMe! Project. You can utilize
*  software of the GroupMe! project under the terms of the 
*  GNU Lesser General Public License as published by the
*  Free Software Foundation in version 2.1.
* 
*  The GNU Lesser General Public License can be found at:
*  http://www.gnu.org/licenses/lgpl.html
*
*  This copyright notice MUST APPEAR in all copies of the file!
***************************************************************/
package net.groupme.export.rdf;

import java.net.URI;
import java.util.Collection;

import javax.sql.DataSource;

import net.groupme.export.ITagExport;
import net.groupme.export.Utils;
import net.groupme.export.constants.General;
import net.groupme.export.constants.TagConstants;
import net.groupme.model.Tag;

import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.DC;
import com.hp.hpl.jena.vocabulary.RDF;

/**
 * This class provides...
 *
 * @author Daniel Plappert
 * @version 0.1
 */
public class TagExport implements IRdfDataExport, ITagExport {
    
    /** contains all tags */
    private Collection<Tag> data = null;
    
    private DataSource ds;
    
    /**
     * Creates a new instance of TagExport
     */
    private TagExport() {}
    
    public TagExport(DataSource ds) {
        this.ds = ds;
    }
    
    
    /**
     * Sets all tags...
     *
     * @param Collection<Tag> All Tags to export
     */
    public void setData(Collection<Tag> data) {
        this.data = data;
    }
    
    
    public void exportData(Model model, boolean exportAll) {
        
        Resource tempRes = null;
        Resource isConcept = null;
        Literal tempLit = null;
        
        for(Tag tempTag : data) {
        
            tempRes = model.createResource(tempTag.getURI().toString());
            tempRes.addProperty(RDF.type, model.createResource(General.tagBaseUri));
            
            // Keyword attribute
            tempLit = model.createTypedLiteral(tempTag.getKeyword());
            tempRes.addProperty(TagConstants.keywordProperty, tempLit);
            
            // Description attribute
            String description =  tempTag.getDescription();
            if(description != null){
            	tempLit = model.createTypedLiteral(tempTag.getDescription());
            	tempRes.addProperty(DC.description, tempLit);
            }
            
            // hasConcept attribute
            //TODO: wie greife ich darauf zu?
            
            // the correlation between the tag, user, group and resource
            setTagCorrelation(model, tempTag.getURI(), tempRes);

        }
        
    }    
    
    
    /**
     *
     *
     * @param Model
     * @param URI
     * @param Resource
     */
    private void setTagCorrelation(Model model, URI uri, Resource res) {
       
        // check if attributes already added
        if ( res.hasProperty(TagConstants.isCorrelatedToProperty) ) {
            return;
        }
        
        for(TaggingContext tempTaggingContext : Utils.getTaggingContextByTagUri(ds, uri)) {

            Resource blankNode = model.createResource();
            blankNode.addProperty(RDF.type, model.createResource(TagConstants.taggingContextUri));
            blankNode.addProperty(TagConstants.groupProperty, model.createResource(tempTaggingContext.getGroup().toString()));
            blankNode.addProperty(TagConstants.userProperty, model.createResource(tempTaggingContext.getUser().toString()));
            blankNode.addProperty(TagConstants.resourceProperty, model.createResource(tempTaggingContext.getResource().toString()));
            blankNode.addProperty(TagConstants.importanceProperty, model.createTypedLiteral(tempTaggingContext.getImportance()));
            blankNode.addProperty(TagConstants.creationDateProperty, model.createTypedLiteral(tempTaggingContext.getCreationDate(), XSDDatatype.XSDdate));

            res.addProperty(TagConstants.isCorrelatedToProperty, blankNode);
        }
        
    }
    
}
